import 'dart:developer';

import 'package:flutter/material.dart';
import '../../utils/layout/layout_responsive.dart';
import './home_swiper.dart';
import '../../utils/G.dart';
import 'home_menus.dart';
import 'home_product.dart';

class Home extends StatefulWidget {
  Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  ///定义数据开始
  Map apiData = {};
  List swiperlist = [];
  List menus = [];

  ///定义数据结束
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: G.api.home.list(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            // case ConnectionState.none: // 初始状态，接口未发起请求
            //   //
            //   break;
            case ConnectionState.waiting: // 等待接口返回结果
              return const Center(
                child: CircularProgressIndicator(),
              );
            case ConnectionState.done: // 请求结束
              if (snapshot.hasData) {
                var res = snapshot.data;
                if (res['status'] == 200) {
                  apiData = res['data'];
                  menus = apiData['menus']['default']['imgList']['list'];
                  swiperlist =
                      apiData['swiperBg']['default']['imgList']['list'];
                  return ListView(
                    shrinkWrap: true,
                    children: [
                      LayoutResponsive.isXS(context) ||
                              LayoutResponsive.isSM(context)
                          ? Column(
                              children: [
                                // 轮播图
                                Container(
                                    height: 180,
                                    child: HomeSwiper(swiperlist: swiperlist)),

                                // 首页菜单导航
                                HomeMenus(menuList: menus),
                              ],
                            )
                          : Row(
                              children: [
                                Expanded(
                                  child: Container(
                                      height: 180,
                                      width: G.width / 2,
                                      child:
                                          HomeSwiper(swiperlist: swiperlist)),
                                ),
                                Expanded(
                                  child: HomeMenus(menuList: menus),
                                )
                              ],
                            ),

                      // 商品列表
                      HomeProduct(),

                      // 页脚
                      G.footer()
                    ],
                  );
                }
              }
              return Center(
                child: CircularProgressIndicator(),
              );
              break;
            default:
              return Container();
          }
        });
  }
}
